<template>
  <div class="note-page">
    <blHeader
      title="相册"
      subtitle="记录生活的点点滴滴，回忆往事"
      backgroundImage="@/assets/img/banner.jpg"
   
    />
    <BlContainer>
      <div class="content">
        <div class="nav-list">
          <nav class="active">最新</nav>
          <nav>日常</nav>
          <nav>美食</nav>
          <nav>宠物</nav>
          <nav>旅行</nav>
          <nav>二次元</nav>
          <nav>其他</nav>
        </div>

        <div class="gallery-list">
          <div class="item" v-for="item in 20" :key="item">
            <div class="item-img" @click="handleViewerImg">
              <img src="https://picsum.photos/300/400" alt="" />
            </div>

            <div class="item-content">
              <h3>IMG900</h3>

              <div class="item-banner">
                <span>2023/08/10</span>
                <div>点赞（20）</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </BlContainer>

    <div class="mask">
      <img id="overImg" src="#" alt="" @click="handleCloseMask" />
      <img
        class="close"
        src="@/assets/svg/close.svg"
        @click="handleCloseMask"
        alt=""
      />
    </div>
  </div>
</template>

<script setup>
const getImageList = () => {
  // 假设父元素的 ID 是 'parentElement'
  const parentElement = document.querySelector(".gallery-list");
  // 使用 querySelectorAll 获取所有子元素中的 <img> 标签
  const images = parentElement.querySelectorAll("img");
  // 遍历获取的 <img> 元素
  images.forEach((img, index) => {
    console.log(`Image ${index + 1}:`, img);
  });
};

const addActiveNavClick = () => {
  const navItems = document.querySelectorAll(".nav-list nav");
  navItems.forEach((nav) => {
    nav.addEventListener("click", function () {
      navItems.forEach((item) => {
        item.classList.remove("active");
      });
      this.classList.add("active");
    });
  });
};

const handleViewerImg = (e) => {
  const target = e.target;

  const images = target.querySelectorAll("img");
  const overImg = document.querySelector("#overImg");
  const mask = document.querySelector(".mask");
  // 禁止页面滚动
  document.body.style.overflow = "hidden";
  // 显示遮罩层
  mask.style.display = "block";
  // 设置图片源
  if (images.length > 0) {
    overImg.setAttribute("src", images[0].src);
  }
};

const handleCloseMask = () => {
  const mask = document.querySelector(".mask");
  document.body.style.overflow = "auto";
  mask.style.display = "none";
  const overImg = document.querySelector("#overImg");
  overImg.removeAttribute("src");
};

onMounted(() => {
  addActiveNavClick();
  // getImageList()
});
</script>

<style lang="less" scoped>
.note-page {
  min-height: 100vh;

  .mask {
    position: fixed;
    width: 100%;
    height: 100%;
    inset: 0;
    z-index: 100000;
    background: @black-alpha;
    display: none;
    touch-action: none;

    #overImg {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      cursor: zoom-out;
    }

    .close {
      position: absolute;
      top: 20px;
      right: 20px;
      width: 30px;
      height: 30px;
      cursor: pointer;
    }
  }

  .content {
    max-width: 1140px;
    margin: 0 auto;

    .nav-list {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 30px;

      nav {
        font-size: 18px;
        color: #0d3d3f;
        padding: 6px 20px;
        box-sizing: border-box;
        font-weight: bold;
        cursor: pointer;
      }

      .active {
        background: #fff;
        border-radius: 8px;
        color: #28b9be;
        border: 2px solid #28b9be;
      }
    }

    .gallery-list {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      gap: 20px;
      margin-top: 60px;

      .item {
        height: 300px;
        width: auto;
        border-radius: 10px;
        overflow: hidden;
        background: #fff;

        .item-img {
          height: 220px;
          position: relative;
          overflow: hidden;
          cursor: zoom-in;

          &::before {
            content: "";
            position: absolute;
            background: linear-gradient(transparent 55%, #fff);
            display: block;
            bottom: 0px;
            left: 0px;
            height: 220px;
            z-index: 1;
            width: 100%;
          }

          img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transform: scale(1) rotate(0deg);
            transition: transform 0.5s;
          }
        }

        &:hover {
          box-shadow: 0px 0px 0px 2px #fff;

          img {
            transform: scale(1.4) rotate(10deg);
          }
        }

        .item-content {
          height: 80px;
          padding: 10px;
          box-sizing: border-box;
          display: flex;
          flex-direction: column;
          justify-content: space-between;

          .item-banner {
            display: flex;
            color: #0d3d3f;
            align-items: flex-end;
            justify-content: space-between;
          }
        }
      }
    }
  }
}
</style>
